using AntiCheat.Web.Models;
using Microsoft.AspNetCore.Mvc;
using Share;
using System.Diagnostics;

namespace AntiCheat.Web.Controllers;

public class HomeController(ILogger<HomeController> logger, IHttpClientFactory httpClientFactory) : Controller
{
    private readonly HttpClient httpClient = httpClientFactory.CreateClient("APIAddress");

    public IActionResult Index()
    {
        return View(new PlainData());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Index(PlainData data)
    {
        logger.LogInformation("{@data}", data);
        using HttpResponseMessage response = await httpClient.PostAsJsonAsync("query", data);
        response.EnsureSuccessStatusCode();
        var result = await response.Content.ReadFromJsonAsync<ResponseResultPlainText>();
        ViewBag.Result = result;
        return View(data);
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}
